<?php

require_once('../model/db.php');
require_once('Classes/Army.class.php');
require_once('Classes/Inland.class.php');

$armyarr = select_from_db(array('unit','action','target'),array('moves'),array('game_id=6'));
$armyprov = select_from_db(array('unitid','province'),array('army'),array('game_id=6'));
$tempprov = "";
$provincearr = select_from_db(array('name'),array('province'),array("mapname='Europe'"));

foreach($provincearr as $province){
	$p = $province['name'];
	$prov[$p] = new Inland($province['name']);
}

foreach($armyarr as $army){
	$army['unit'] = trim($army['unit'], '#');
	for($i=0;$i<count($armyprov);$i++){
		if($armyprov[$i]['unitid']==$army['unit']){
			$tempprov = $armyprov[$i]['province'];
		}
	}
	$a = $army['unit'];
	$units[$a] = new Army($army['unit'],$army['action'],$army['target'],$tempprov);
}
foreach($units as $unit){
	$target = $unit -> getTarget();
	$action = $unit -> getAction();
	if($action == 'Hold'){
		$prov[$target] -> setDef_unit($unit -> getId());
	}else if($action == 'move'){
		$prov[$target] -> setAtk_unit($unit -> getId());
	}
}

//foreach($prov as $p){
//	echo $p -> getName() . " has defunit: " . $p -> getDef_unit() . "<br>";
//	echo $p -> getName() . " has atkunit: " . $p -> getAtk_unit() . "<br><br>";
//}

//print_r($prov);

foreach($prov as $p){
	$p -> resolve();
}

?>